//
//  JMCPIListVC.m
//  NewiOSProject
//
//  Created by MAC on 2023/9/8.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMCPIListVC.h"
#import "JMCPICell.h"
#import "JMCPIModel.h"
@interface JMCPIListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong) NSArray *dataList;

@end

@implementation JMCPIListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.TitleLabel.text = @"Productos recomendados";
    [self.view addSubview:self.tableView];
    [self getDataList];
}

-(void)getDataList{
        
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(@"/api/index/product-cpi2") params:nil success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        [self stopAnimating];
        self.dataList = [JMCPIModel mj_objectArrayWithKeyValuesArray:dataDic[@"productList"]];
        [self.tableView reloadData];
    } fail:^(NSString * _Nonnull errorMsg) {
        [self stopAnimating];
    }];
}
//MARK: -UITableViewDataSource UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 6;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMCPICell *cell = [tableView dequeueReusableCellWithIdentifier:@"JMCPICell" forIndexPath:indexPath];
    if (indexPath.row < self.dataList.count) {
        JMCPIModel *model = self.dataList[indexPath.row];
           cell.model = model;
        cell.selectionStyle = 0;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 124;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    JMCPIModel *model = self.dataList[indexPath.row];
   
}

//MARK: -lazyLoad

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT(), kAppW(), kAppH()-NAVIGATION_BAR_HEIGHT()) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[JMCPICell class] forCellReuseIdentifier:@"JMCPICell"]; // 注册UITableViewCell
        _tableView.separatorColor = UIColor.clearColor;
        _tableView.sectionHeaderTopPadding = 0;
        _tableView.backgroundColor = Hex_Str_COLOR(@"#F7F7F5");
    }
    return _tableView;
}
@end
